import request from '@/utils/login'
import { hex_md5 } from '@/utils/md5'
import { decrypt } from '@/utils/rsaEncrypt'

// 获取验证码图片
export function getCodeImg() {
  return request({
    url: 'weiqiao/vcode',
    method: 'get'
  })
}

// 绑定微信
export function bindWxSubmit(req) {
  return request({
    url: 'weiqiao/qrlogin/callbackbind',
    method: 'post',
    data: req
  })
}

// 获取微信登录的config信息
export function getWeiXinBindConfig() {
  return request({
    url: 'weiqiao/qrlogin/getbindconfig',
    method: 'get'
  })
}

// 获取微信登录的config信息
export function getWeiXinConfig() {
  return request({
    url: 'weiqiao/qrlogin/getloginconfig',
    method: 'get'
  })
}

// 获取微信登录的config信息
export function WxLoginSubmit(req) {
  return request({
    url: 'weiqiao/qrlogin/callbacklogin',
    method: 'post',
    data: req
  })
}

// 手机验证码登录
export function phoneLogin(req) {
  return request({
    url: 'weiqiao/phone-login',
    method: 'post',
    data: req
  })
}

// 获取手机验证码
export function getPhoneCode(req) {
  return request({
    url: 'weiqiao/generate-code',
    method: 'post',
    data: req
  })
}

// 获取手机验证码
export function getCode(phone, type, isInDb) {
  const timestamp = Math.round(new Date())
  const req = {
    phone,
    type,
    timestamp,
    signcode: hex_md5(phone + type + isInDb + timestamp)
  }
  return request({
    url: 'weiqiao/generate-code',
    method: 'post',
    data: req
  })
}

// 修改手机号
export function updatePhone(phone, code, token) {
  const timestamp = Math.round(new Date())
  const req = {
    phone,
    code,
    timestamp,
    token,
    signcode: hex_md5(token + phone + code + timestamp)
  }
  return request({
    url: 'weiqiao/update-phone',
    method: 'post',
    data: req
  })
}

// 修改密码
export function updatePass(phone, oldPassword, password) {
  const timestamp = Math.round(new Date()) + ''
  const code = '123456'
  const data = {
    phone,
    oldPassword,
    password,
    code,
    timestamp,
    signcode: hex_md5(phone + oldPassword + password + code + timestamp)
  }
  return request({
    url: 'weiqiao/update-passwords',
    method: 'post',
    data
  })
}

// 用户登录 验证码码 后端返回的uuid
export function userLogin(username, password, vcode, uuid) {
  const timestamp = Math.round(new Date())
  const req = {
    username,
    password: decrypt(password),
    timestamp,
    vcode,
    uuid,
    signcode: hex_md5(username + decrypt(password) + timestamp)
  }
  return request({
    url: 'weiqiao/login',
    method: 'post',
    data: req
  })
}

// 体验账号登录
export function experienceLogin(username, password) {
  const timestamp = Math.round(new Date())
  const req = {
    username,
    password: password,
    timestamp,
    signcode: hex_md5(username + password + timestamp)
  }
  return request({
    url: 'weiqiao/experienceLogin',
    method: 'post',
    data: req
  })
}

// 注销
export function loginOut(token) {
  const timestamp = Math.round(new Date())
  const req = {
    token,
    timestamp,
    signcode: hex_md5(token + timestamp)
  }
  return request({
    url: 'weiqiao/logout',
    method: 'post',
    data: req
  })
}

// 登录校验
export function loginCheck(token) {
  const timestamp = Math.round(new Date())
  const req = {
    token,
    timestamp,
    signcode: hex_md5(token + timestamp)
  }
  return request({
    url: 'weiqiao/logincheck',
    method: 'post',
    data: req
  })
}

// 注册
export function register(req) {
  return request({
    url: 'weiqiao/register',
    method: 'post',
    data: req
  })
}

// 找回密码
export function findPass(req) {
  return request({
    url: 'weiqiao/update-password',
    method: 'post',
    data: req
  })
}

// 获取用户信息
export function getInfo() {
  return request({
    url: 'auth/info',
    method: 'get'
  })
}
